package com.zhongwang.mms.module.allocation.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.allocation.model.AllocationInOutWar;
import com.zhongwang.mms.module.allocation.model.AllocationOrderModel;
import com.zhongwang.mms.module.allocation.service.AllocationOrderService;
import com.zhongwang.mms.module.basic.dao.TWarehouseInfoDao;
import com.zhongwang.mms.module.basic.service.MMaterialDictionaryService;
import com.zhongwang.mms.module.basic.service.TDeptService;
import com.zhongwang.mms.module.basic.service.TWarehouseInfoService;
import com.zhongwang.mms.module.material.model.ComplexStorageModel;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:调拨单管理
 * @author: th
 * @time: 2020/8/11
 */
@Controller
@RequestMapping("/allocationOrder")
public class AllocationOrderController extends BaseController {

    //页面文件路径
    private String prefix = "allocation/allocationOrder";
    @Autowired
    private AllocationOrderService allocationOrderService;
    @Autowired
    private TWarehouseInfoService tWarehouseInfoService;
    @Autowired
    private AllocationOrderDao allocationOrderDao;
    @Autowired
    private MMaterialDictionaryService mMaterialDictionaryService;
    @Autowired
    private TDeptService deptService;
    @Autowired
    private TWarehouseInfoDao tWarehouseInfoDao;

    //加载页面
    @GetMapping("/page")
    @RequiresPermissions({"allocation:order:vew"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";
    }

    /**
     * 调拨单管理页面数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult list(AllocationOrderModel query){

        startPage();
        List<AllocationOrderModel> list = allocationOrderService.selectAllByQuery(query);
        return jsonResult(list);
    }

    /**
     * 根据调拨类型获取出入库仓库
     * @param odrType
     * @return
     */
    @ResponseBody
    @PostMapping("/findWar")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult findWar(Integer odrType){
        AllocationInOutWar allocationInOutWar = new AllocationInOutWar();
        if(odrType == 1){
            allocationInOutWar.setOutWar(tWarehouseInfoService.findWarByAllocationType(1));
            allocationInOutWar.setInWar(tWarehouseInfoService.findWarByAllocationType(1));
        }else if(odrType == 2){
            allocationInOutWar.setOutWar(tWarehouseInfoService.findWarByAllocationType(2));
            allocationInOutWar.setInWar(tWarehouseInfoService.findWarByAllocationType(2));
        }else if(odrType == 3){
            allocationInOutWar.setOutWar(tWarehouseInfoService.findWarByAllocationType(1));
            allocationInOutWar.setInWar(tWarehouseInfoService.findWarByAllocationType(3));
        }
        return JsonResult.success().setData(allocationInOutWar);
    }

    /**
     * 创建调拨单保存
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/saveAllocationOrder")
    @RequiresPermissions({"allocation:order:add"})
    public JsonResult saveAllocationOrder(AllocationOrderModel param){

        return allocationOrderService.saveAllocationOrder(param);
    }

    /**
     * 点击编辑页面跳转
     * @param odrId
     * @return
     */
    @GetMapping("/allocationOrderDetail/{id}")
    public String allocationOrderDetail(Model model,@PathVariable("id") Integer odrId) {

        AllocationOrderModel allocationOrder = allocationOrderDao.selectAllByPrimaryKey(odrId);
        model.addAttribute("ardOdrId",odrId);
        model.addAttribute("odrCode",allocationOrder.getOdrCode());
        model.addAttribute("odrType",allocationOrder.getOdrType());
        model.addAttribute("outWarId",allocationOrder.getOdrOutWhiId());
        model.addAttribute("outWarName",allocationOrder.getOdrOutWhiName());
        model.addAttribute("inWarId",allocationOrder.getOdrInWhiId());
        model.addAttribute("inWarName",allocationOrder.getOdrInWhiName());
        model.addAttribute("odrMakePersonId",allocationOrder.getOdrMakePersonId());
        model.addAttribute("odrMakeDate",new SimpleDateFormat("yyyy-MM-dd").format(allocationOrder.getOdrMakeDate()));
        if(allocationOrder.getOdrType() == 1){
            return prefix + "/purDetail";
        }else if(allocationOrder.getOdrType() == 2){
            model.addAttribute("matType", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialTypeList());
            model.addAttribute("matQuality", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialQualityList());
            model.addAttribute("matSpec", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialSpecList());
            model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
            model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
            model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
            return prefix + "/matDetail";
        }else{
            return prefix + "/halDetail";
        }
    }

    /**
     * 点击查看页面跳转
     * @param odrId
     * @return
     */
    @GetMapping("/allocationOrderDetailView/{id}")
    @RequiresPermissions({"allocation:order:vew"})
    public String allocationOrderDetailView(Model model,@PathVariable("id") Integer odrId) {

        AllocationOrderModel allocationOrder = allocationOrderDao.selectAllByPrimaryKey(odrId);
        model.addAttribute("ardOdrId",odrId);
        model.addAttribute("odrCode",allocationOrder.getOdrCode());
        model.addAttribute("odrType",allocationOrder.getOdrType());
        model.addAttribute("outWarId",allocationOrder.getOdrOutWhiId());
        model.addAttribute("outWarName",allocationOrder.getOdrOutWhiName());
        model.addAttribute("inWarId",allocationOrder.getOdrInWhiId());
        model.addAttribute("inWarName",allocationOrder.getOdrInWhiName());
        model.addAttribute("odrMakePersonId",allocationOrder.getOdrMakePersonId());
        model.addAttribute("odrMakeDate",new SimpleDateFormat("yyyy-MM-dd").format(allocationOrder.getOdrMakeDate()));
        if(allocationOrder.getOdrType() == 1){
            return prefix + "/purDetailView";
        }else if(allocationOrder.getOdrType() == 2){
            model.addAttribute("ardOdrId",odrId);
            model.addAttribute("matType", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialTypeList());
            model.addAttribute("matQuality", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialQualityList());
            model.addAttribute("matSpec", mMaterialDictionaryService.findMaterialDictionaryByKindName("").getMaterialSpecList());
            model.addAttribute("odrMakePerson", ShiroUtils.getUser().getName());
            model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            model.addAttribute("dept", deptService.findDeptInfo(new TDept()));
            model.addAttribute("warehouse", tWarehouseInfoDao.findWarehouseInfoListByWhiType("原料库"));
            return prefix + "/matDetailView";
        }else{
            return prefix + "/halDetailView";
        }
    }

    /**
     * 外购件编辑加载数据
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/purInv")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult purInv(AllocationOrderModel param){

        startPage();
        List<AllocationOrderModel> list = allocationOrderService.selectOrderDetailByArdOdrId(param.getArdOdrId());
        return jsonResult(list);
    }

    /**
     * 外购件库存信息
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/purInvList")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult purInvList(PInventoryModel param){

        startPage();
        List<PInventoryModel> list = allocationOrderService.selectPurInvList(param);
        return jsonResult(list);
    }

    /**
     * 原料库库存信息
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/matInvList")
    @RequiresPermissions({"allocation:order:vew"})
    public JsonResult matInvList(ComplexStorageModel param){

        startPage();
        List<ComplexStorageModel> list = allocationOrderService.selectMatInvList(param);
        return jsonResult(list);
    }

    /**
     * 保存调拨单明细
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/saveDetail")
    @RequiresPermissions({"allocation:order:edit"})
    public JsonResult saveDetail(@RequestBody List<AllocationOrderModel> param){

        return allocationOrderService.saveDetail(param);
    }

    /**
     * 修改数量保存
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/updateAllocationOrderDetail")
    @RequiresPermissions({"allocation:order:edit"})
    public JsonResult updateAllocationOrderDetail(AllocationOrderModel param){

        return jsonResult(allocationOrderService.updateAllocationOrderDetail(param));
    }

    /**
     * 删除调拨明细表数据
     * @param ardId
     * @return
     */
    @ResponseBody
    @PostMapping("/delDetail")
    @RequiresPermissions({"allocation:order:del"})
    public JsonResult delDetail(@Param("ardId") Integer ardId){

        return jsonResult(allocationOrderService.delAllocationOrderDetail(ardId));
    }

    /**
     * 提交调拨单
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/commit")
    @RequiresPermissions({"allocation:order:edit"})
    public JsonResult commit(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.commitAllocationOrderByPrimaryKey(odrId));
    }

    /**
     * 删除调拨单
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"allocation:order:del"})
    public JsonResult del(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.delAllocationOrderByPrimaryKey(odrId));
    }

    /**
     * 撤销调拨单提交
     * @param odrId
     * @return
     */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"allocation:order:edit"})
    public JsonResult reject(@Param("odrId") Integer odrId){

        return jsonResult(allocationOrderService.rejectAllocationOrderByPrimaryKey(odrId));
    }
}
